package com.example.chenjun.myapplication;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimatedStateListDrawable;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.graphics.drawable.Animatable2Compat;
import android.support.graphics.drawable.AnimatedVectorDrawableCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.transition.Explode;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.ResourceDrawableDecoder;
import com.example.chenjun.myapplication.activity.BaseActivity;
import com.example.chenjun.myapplication.activity.SearchActivity;
import com.example.chenjun.myapplication.fragment.BannerFragment;
import com.example.chenjun.myapplication.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private TextView mTvSearch;
    private int index;
    private Animatable mAnimatable;
    private boolean isDefault;
    private AnimatedVectorDrawable mAnimation2, mAnimation1;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        super.onCreate(savedInstanceState);
        mTvSearch = findViewById(R.id.tv_search);
        mAnimation1 = (AnimatedVectorDrawable) getDrawable(R.drawable.vector_animation);
        mAnimation2 = (AnimatedVectorDrawable) getDrawable(R.drawable.back_vector_animation);
        final ImageView iv = findViewById(R.id.iv_vector);
        Drawable drawable = iv.getDrawable();
        if (drawable instanceof Animatable){
            mAnimatable = (Animatable) drawable;
        }
        Fragment homeFragment = Fragment.instantiate(this, HomeFragment.class.getCanonicalName());
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fm_home_container, homeFragment).commit();

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isDefault){
                    iv.setImageDrawable(mAnimation1);
                    mAnimatable = mAnimation1;
                }else {
                    iv.setImageDrawable(mAnimation2);
                    mAnimatable = mAnimation2;
                }
                if (mAnimatable != null){
                    mAnimatable.start();
                }
                isDefault = !isDefault;
            }
        });
        mTvSearch.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
//                Drawable background = mTvSearch.getBackground();
//                if (background instanceof Animatable){
//                    mTvSearch.animate()
//                            .alpha(0)
//                            .setDuration(300)
//                            .start();
//                    ((Animatable) background).start();
//                }
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });
    }

    @Override
    protected int getResId() {
        return R.layout.app_activity_main;
    }
}
